﻿#include <stdio.h>
#include <stdlib.h>
#include "uv.h"

uv_loop_t loop;
uv_udp_t client;
sockaddr_in laddr,raddr;
int nerror;

void on_resolved(uv_getaddrinfo_t* req, int status, struct addrinfo* res){
    if (status < 0) {
        fprintf(stderr, "getaddrinfo callback error %s\n", uv_err_name(status));
        return;
    }
    char addr[17] = {'\0'};
    uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16);
    fprintf(stdout, "IP: %s\n", addr);
}

int main(int argc, char* argv[]){
    fprintf(stdout, "dns demo\n");
    uv_loop_init(&loop);

    addrinfo hints;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = 0;

    uv_getaddrinfo_t req_resolver;

    char* pHost = "www.baidu.com";
    if(argc > 1){
        pHost = argv[1];
    }
    uv_getaddrinfo(&loop, &req_resolver, on_resolved, pHost, "80", &hints);

    fprintf(stdout, "run\n");
    nerror = uv_run(&loop, UV_RUN_DEFAULT);

    uv_loop_close(&loop);
    return nerror;
}

